﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Stu> students = new List<Stu>()
            {
                new Stu {Id = 1, Name = "zhang"},
                new Stu {Id = 2, Name = "li"}
            };

            var rst = students.FirstOrDefault(x => x.Id == 1);
            Console.WriteLine(rst.Id + rst.Name);
            //使用Where().FirstOrDefault 比使用FirstOrDefault慢
            var rst2 = students.Where(x => x.Id == 2).FirstOrDefault();
            Console.WriteLine(rst2.Id + rst2.Name);

            var t = typeof(Stu).IsValueType; //是否是值类型:true

            //因为Stu 不是值类型，所以没有符合条件的时候返回null
            var rst3 = students.FirstOrDefault(x => x.Id == 3);
            Console.WriteLine(rst3.Id + rst3.Name);

            //使用linq 返回的也是null
            var l = from s in students
                where s.Id == 2
                select new { s.Id, s.Name };

            Console.ReadKey();


        }
    }


    public class Stu
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
